package com.cyl.game.demos.web.service.Impl;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cyl.game.demos.web.mapper.UserHeroRelationMapper;
import com.cyl.game.demos.web.mapper.UserMapper;
import com.cyl.game.demos.web.entity.Hero;
import com.cyl.game.demos.web.entity.User;
import com.cyl.game.demos.web.entity.UserHeroRelation;
import com.cyl.game.demos.web.service.UserService;
import javax.annotation.Resource;

@Service()
@Transactional
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserHeroRelationMapper userHeroRelationMapper;
    @Override
    public int addUser(User user) {
        return userMapper.insertUser(user);
    }

    @Override
    public User getUserByUsername(String username) {
        return userMapper.selectUserByUsername(username);
    }

    @Override
    public List<User> getAllUsers() {
        return userMapper.selectAllUsers();
    }
    @Override
    public List<Integer> getHeroIdsByUserId(Integer user_id) {
        return userMapper.selectHeroIdsByUserId(user_id);
    }

    @Override
    public Map<Integer, List<Integer>> getAllUsersWithHeroIds() {
        List<Map<String, Object>> userMaps = userMapper.selectAllUsersWithHeroIds();
        Map<Integer, List<Integer>> result = new HashMap<Integer, List<Integer> >();
        for (Map<String, Object> userMap : userMaps) {
            Integer user_id = (Integer) userMap.get("user_id");
            String heroIdsStr = (String) userMap.get("hero_ids");
            List<Integer> heroIdList = new ArrayList<Integer>();
            if (heroIdsStr!= null &&!heroIdsStr.isEmpty()) {
                String[] idsArray = heroIdsStr.split(",");
                for (String id : idsArray) {
                    heroIdList.add(Integer.parseInt(id));
                }
            }
            result.put(user_id, heroIdList);
        }
        return result;
    }
    @Override
    public void addHeroForUser(int user_id, int hero_id) {
        UserHeroRelation relation = new UserHeroRelation();
        relation.setUser_id(user_id);
        relation.setHero_id(hero_id);
        userHeroRelationMapper.insertUserHeroRelation(relation);
    }

    @Override
    public void removeHeroFromUser(int user_id, int hero_id) {
        // 这里可以先查询出对应的关联记录的relation_id（假设只有一条关联记录的简单情况，复杂情况需进一步处理）
        // 然后调用deleteUserHeroRelationById方法删除关联记录，以下是简单示例代码，实际可能需完善查询逻辑
        UserHeroRelation relation = userHeroRelationMapper.selectRelationByUserIdAndHeroId(user_id, hero_id);
        if (relation!= null) {
            userHeroRelationMapper.deleteUserHeroRelationById(relation.getRelation_id());
        }
    }

    @Override
    public List<Hero> getHeroesOwnedByUserId(int user_id) {
        return userMapper.selectAllHeroesOwnedByUserId(user_id);
    }

    @Override
    public List<User> getUsersWhoOwnHero(int hero_id) {
        return userMapper.selectAllUsersWhoOwnHero(hero_id);
    }
}